package org.example.springboot.utils;

import org.springframework.security.crypto.bcrypt.BCrypt;

public class BcryptTool {

    /**
     * 使用BCrypt算法生成密码的哈希值
     * @param password 明文密码
     * @return 哈希值
     */
    public static String hashPassword(String password) {
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    /**
     * 校验密码是否匹配存储的哈希值
     * @param password 明文密码
     * @param hashed 哈希值
     * @return 如果匹配返回true，否则返回false
     */
    public static boolean checkPassword(String password, String hashed) {
        return BCrypt.checkpw(password, hashed);
    }

    public static void main(String[] args) {
        // 示例用法
        String password = "123456";
        String hashed = hashPassword(password);

        System.out.println("Hashed Password: " + hashed);

        boolean isPasswordValid = checkPassword(password, "$2a$10$6Q6VKufhYK5khWBZGbQcOOmZY9oGu6hBg1rJPovRX0OOZ4vv1givy");
        System.out.println("Password is valid: " + isPasswordValid);
    }
}
